package com.tcm.doctor.service;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.core.dozer.DozerConvert;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.tcm.doctor.domain.dto.DoctorAccountDto;
import com.tcm.doctor.domain.dto.DoctorFlowDto;
import com.tcm.doctor.domain.entity.DoctorAccount;
import com.tcm.doctor.domain.enums.BusinessCodeEnum;
import com.tcm.doctor.domain.vo.DoctorAccountVo;
import com.tcm.doctor.mapper.DoctorAccountMapper;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 6000, delay = -1)
public class DoctorAccountServiceImpl implements DoctorAccountService {

    @Autowired
    private DoctorAccountMapper doctorAccountMapper;

    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private DoctorFlowService doctorFlowService;

    /**
     * 获得单条信息
     *
     * @param id
     * @return Doctor
     */
    @Override
    public DoctorAccountVo findById(Long id) {
        DoctorAccount doctorAccount = doctorAccountMapper.findById(id);
        DoctorAccountVo doctorAccountVo = null;
        if (ObjectUtil.isNotEmpty(doctorAccount)) {
            doctorAccountVo = DozerConvert.copyProperties(doctorAccount, DoctorAccountVo.class);
        }
        return doctorAccountVo;
    }

    /**
     * 根据用户id获取医生账户信息
     * @param userId
     * @return
     */
    @Override
    public DoctorAccountVo findByUserId(Long userId) {
        DoctorAccount doctorAccount = doctorAccountMapper.findByUserId(userId);
        DoctorAccountVo doctorAccountVo = new DoctorAccountVo();
        if(ObjectUtil.isNotEmpty(doctorAccount)){
            doctorAccountVo = DozerConvert.copyProperties(doctorAccount, DoctorAccountVo.class);
        }
        return doctorAccountVo;
    }


    /**
     * 新增信息
     *
     * @param doctorAccountDto
     */
    @Override
    public void save(DoctorAccountDto doctorAccountDto) throws Exception {

        if (doctorAccountDto.getUserId().equals(0L)) {
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        DoctorAccount doc = doctorAccountMapper.findByUserId(doctorAccountDto.getUserId());

        DoctorAccount doctorAccount = DozerConvert.copyProperties(doctorAccountDto, DoctorAccount.class);
        doctorAccount.setUpdateTime(LocalDateTime.now());
        if (ObjectUtil.isEmpty(doc)) {
            doctorAccount.setId(uidGenerator.getUID());
            doctorAccount.setCreateTime(LocalDateTime.now());
            doctorAccountMapper.insert(doctorAccount);
        } else {
            doctorAccountMapper.update(doctorAccount);
        }

    }


    /**
     * 永久删除
     *
     * @param id
     */
    @Override
    public void deleteDoctor(Long id) {
        doctorAccountMapper.deleteById(id);
    }

    @Override
    public void addReward(Long userId, BigDecimal price) throws Exception{

        DoctorAccount doc = doctorAccountMapper.findByUserId(userId);
        if (ObjectUtil.isEmpty(doc)) {
            DoctorAccount doctorAccount = new DoctorAccount();
            doctorAccount.setId(uidGenerator.getUID());
            doctorAccount.setUserId(userId);
            doctorAccount.setCurrentMoney(price);
            doctorAccount.setRewardFee(price);
            doctorAccount.setTotalMoney(price);
            doctorAccount.setUpdateTime(LocalDateTime.now());
            doctorAccount.setCreateTime(LocalDateTime.now());
            doctorAccountMapper.insert(doctorAccount);
        } else {
            doc.setCurrentMoney(NumberUtil.add(doc.getCurrentMoney(),price));
            doc.setRewardFee(NumberUtil.add(doc.getRewardFee(),price));
            doc.setTotalMoney(NumberUtil.add(doc.getTotalMoney(),price));
            doc.setUpdateTime(LocalDateTime.now());
            doctorAccountMapper.update(doc);
        }

        DoctorFlowDto doctorFlowDto = new DoctorFlowDto();
        doctorFlowDto.setUserId(userId);
        doctorFlowDto.setType(new Short("2"));
        doctorFlowDto.setMoney(price);
        doctorFlowDto.setMemo("患者赞赏");
        doctorFlowService.save(doctorFlowDto);

    }

}

